﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Microsoft.Win32;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {

            MessageBox.Show("okokokokokok", "hhhhhh");

        }

        //遍历所有子项
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            RegistryKey registry = Registry.LocalMachine;
            RegistryKey regkey = registry.OpenSubKey(@"SOFTWARE");
            foreach (var subkey in regkey.GetSubKeyNames())
            {
                MessageBox.Show(string.Format("子项名：{0}", subkey), "regfind");
                RegistryKey regiskey = regkey.OpenSubKey(subkey);
            }
        }

        //根键里创建一个子项HW，并给它设置一个值项value:abc
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            RegistryKey rklm = Registry.LocalMachine;
            RegistryKey hardware = rklm.OpenSubKey(@"HARDWARE", true);
            RegistryKey hwkey = hardware.CreateSubKey("HW");
            hwkey.SetValue("value", "abcdE");   
        }

        //删除子项HW里面的值项value
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            //主要用到了DeleteValue(),表示删除名称为"Name"的键值，第二个参数表示是否抛出异常
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey hardware = hklm.OpenSubKey(@"HARDWARE", true);
            RegistryKey hwkey = hardware.CreateSubKey("HW");
            hwkey.DeleteValue(@"value", true);
            hklm.Close();
            hardware.Close();
        }

        //修改子项HW里面的值项value的值为123，若不存在这个值项则添加它到HW子项下
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            // 待修改的值项的路径
            string changeKey = @"HARDWARE\HW";

            RegistryKey hardware = Registry.LocalMachine.OpenSubKey(changeKey, true);
            hardware.SetValue("value", "1BD");
        }

        //删除子项HW及其它里面的所有值项
        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            RegistryKey hardware = Registry.LocalMachine.OpenSubKey(@"HARDWARE", true);
            hardware.DeleteSubKey("HW", true);
        }

        //设置开机自启动项
        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            // 开机自启动项的注册表路径
            string runKeyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

            // 新的开机自启动项名称和值（程序路径）
            string appName = "OpenVPN-GUI";
            string appPath = @"C:\Program Files\OpenVPN\bin\openvpn-gui.exe";

            RegistryKey startKey = Registry.LocalMachine.OpenSubKey(runKeyPath, true);

            // 创建新的开机自启动项
            startKey.SetValue(appName, appPath);
        }

        //删除开机自启动项
        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            // 开机自启动项的注册表路径
            string runKeyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

            // 新的开机自启动项名称和值（程序路径）
            string appName = "OpenVPN-GUI";

            RegistryKey startKey = Registry.LocalMachine.OpenSubKey(runKeyPath, true);

            // 删除开机自启动项
            startKey.DeleteValue(appName, true);
        }
    }
}
